﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Borton.Beans
{
    public class MultiIdentifier_ : Bean
    {
        protected String relationId;
        protected String relationReference;
        protected Type relation;
        protected Bean bean = null;
        
        public MultiIdentifier_(String relationId, String relationReference, Type relation) : base()
        {
            this.relationId = relationId;
            this.relationReference = relationReference;
            this.relation = relation;
        }

        private void initBean()
        {
            if (bean == null) {
                bean = (Bean)Activator.CreateInstance(relation);
                if (relationReference.Length > 0)
                {
                    bean = ((Identifier)bean.getField(relationReference)).getBean();
                }
            }
        }

        public override String getTableName()
        {
            initBean();
            return bean.getTableName();
        }

        public override Field[] getFields()
        {
            initBean();
            return bean.getFields();
        }

        public override Bean[] getReferringBeans()
        {
            initBean();
            return bean.getReferringBeans();
        }

        public override Field getField(String columnName)
        {
            initBean();
            return bean.getField(columnName);
        }

        public override Field getId()
        {
            initBean();
            return bean.getId();
        }

        public override String getDisplayName()
        {
            initBean();
            return bean.getDisplayName();
        }

        //TODO
        public override void saveState()
        {
            for (int i = 0; i < getFields().Length; i++)
            {
                getFields()[i].saveState();
            }
        }

        public override object Clone()
        {
            return new MultiIdentifier_(relationId, relationReference, relation);
        }

    }
}
